<template>
  <div style="width: 100%">
    <div style="width: 250px; float: left">
      <a-card
        @contextDictionary.prevent=""
        size="small"
        class="eip-admin-card-small"
      >
        <div slot="title">
          <a-space>
            <a-button type="primary" @click="reload(true)"
              ><a-icon type="redo" />刷新</a-button
            >
            <a-button type="primary"
              ><a-icon type="fullscreen-exit" />折叠</a-button
            >
          </a-space>
        </div>

        <a-spin :spinning="tree.loading" :delay="0">
          <a-directory-tree
            :style="tree.style"
            :tree-data="tree.data"
            :icon="treeIcon"
            @select="treeSelect"
          >
          </a-directory-tree>
        </a-spin>
      </a-card>
    </div>
    <div style="float: left; margin-left: 10px; width: calc(100% - 260px)">
      <a-card class="margin-bottom-sm eip-admin-card-small">
        <eip-search
          ref="search"
          :option="table.search.option"
          @search="tableSearch"
        ></eip-search>
      </a-card>

      <a-card class="eip-admin-card-small">
        <vxe-toolbar ref="toolbar" custom print :refresh="{ query: tableInit }">
          <template v-slot:buttons>
            <eip-toolbar
              @del="del"
              @update="update"
              @add="add"
              @postUser="postUser"
              @menu="menu"
              @menuButton="menuButton"
              @data="data"
              @menu-button-data="menuMenuButtonData"
              @onload="toolbarOnload"
            ></eip-toolbar>
          </template>
        </vxe-toolbar>
        <vxe-table
          ref="table"
          border
          resizable
          size="small"
          :seq-config="{
            startIndex: (table.page.param.current - 1) * table.page.param.size,
          }"
          :checkbox-config="{ trigger: 'row', highlight: true, range: true }"
          :print-config="{}"
          :loading="table.loading"
          :height="table.height"
          :data="table.data"
          :toolbar-config="table.toolbar"
          :tooltip-config="{ showAll: true, contentMethod: tableTooltip }"
          :sort-config="{
            trigger: 'cell',
            defaultSort: { field: 'orderNo', order: 'asc' },
            orders: ['desc', 'asc'],
          }"
          @sort-change="tableSort"
        >
          <vxe-table-column
            type="checkbox"
            width="60"
            align="center"
          ></vxe-table-column>
          <vxe-table-column
            type="seq"
            title="序号"
            width="60"
          ></vxe-table-column>
          <vxe-table-column field="name" title="名称"> </vxe-table-column>
          <vxe-table-column field="parentIdsName" title="归属组织">
          </vxe-table-column>
          <vxe-table-column
            field="isFreeze"
            title="禁用"
            align="center"
            width="80"
            sortable
          >
            <template v-slot="{ row }">
              <a-switch :checked="row.isFreeze" />
            </template>
          </vxe-table-column>
          <vxe-table-column
            field="orderNo"
            title="排序"
            align="center"
            width="80"
            sortable
          >
          </vxe-table-column>
          <vxe-table-column field="remark" title="备注" showOverflow="ellipsis">
          </vxe-table-column>
          <vxe-table-column
            title="操作"
            align="center"
            fixed="right"
            width="100px"
          >
            <template v-slot:default="{ row }">
              <a-tooltip
                @click="tableUpdateRow(row)"
                title="编辑"
                v-if="visible.update"
              >
                <label class="text-eip eip-cursor-pointer">编辑</label>
              </a-tooltip>
              <a-divider type="vertical" v-if="visible.update" />
              <a-tooltip
                title="删除"
                v-if="visible.del"
                @click="tableDelRow(row)"
              >
                <label class="text-red eip-cursor-pointer"> 删除 </label>
              </a-tooltip>
            </template>
          </vxe-table-column>
        </vxe-table>

        <a-pagination
          class="padding-top-sm float-right"
          v-model="table.page.param.current"
          size="small"
          show-size-changer
          show-quick-jumper
          :page-size-options="table.page.sizeOptions"
          :show-total="(total) => `共 ${total} 条`"
          :page-size="table.page.param.size"
          :total="table.page.total"
          @change="tableInit"
          @showSizeChange="tableSizeChange"
        >
        </a-pagination>
      </a-card>
    </div>
    <edit
      ref="edit"
      :visible="edit.visible"
      :data="edit.data"
      :title="edit.title"
      :postId="edit.postId"
      @close="close"
      @save="perateStatus"
    ></edit>

    <eip-chosen-user
      ref="chosenuser"
      :visible="toolbar.chosenuser.visible"
      :title="toolbar.chosenuser.title"
      :data="tree.data"
      @close="chosenUserClose"
    ></eip-chosen-user>

    <eip-permission-menu
      ref="permissionMenu"
      :visible="permission.menu.visible"
      :title="permission.menu.title"
      @close="permissionMenuClose"
    ></eip-permission-menu>

    <eip-permission-menu-button
      ref="permissionMenuButton"
      :visible="permission.menuButton.visible"
      :title="permission.menuButton.title"
      @close="permissionMenuButtonClose"
    ></eip-permission-menu-button>

    <eip-permission-data
      ref="permissionData"
      :visible="permission.data.visible"
      :title="permission.data.title"
      @close="permissionDataClose"
    ></eip-permission-data>

    <eip-permission-menu-menuButton-data
      ref="permissionMenuMenuButtonData"
      :visible="permission.menuMenuButtonData.visible"
      :title="permission.menuMenuButtonData.title"
      @close="permissionMenuMenuButtonDataClose"
    ></eip-permission-menu-menuButton-data>
  </div>
</template>

<script>
import { tree, query, del } from "@/services/system/post/list";
import edit from "./edit";
import eipSearch from "@/pages/common/usercontrol/eip-search";
import eipToolbar from "@/pages/common/usercontrol/eip-toolbar";
import eipChosenUser from "@/pages/common/usercontrol/eip-chosen-user";
import XEUtils from "xe-utils";
import { selectTableRow, deleteConfirm } from "@/utils/util";

import eipPermissionMenu from "@/pages/system/permission/menu";
import eipPermissionMenuButton from "@/pages/system/permission/menubutton";
import eipPermissionData from "@/pages/system/permission/data";
import eipPermissionMenuMenuButtonData from "@/pages/system/permission/menu-menubutton-data";
export default {
  components: {
    edit,
    eipChosenUser,
    eipSearch,
    eipToolbar,
    eipPermissionMenu,
    eipPermissionMenuButton,
    eipPermissionData,
    eipPermissionMenuMenuButtonData,
  },
  data() {
    return {
      tree: {
        style: {
          overflow: "auto",
          height: window.innerHeight - 194 + "px",
        },
        data: [],
        loading: true,
        right: {
          item: null,
          style: "",
        },
      },

      toolbar: {
        chosenuser: {
          visible: false,
          title: "",
        },
      },

      table: {
        columns: [],
        toolbar: {
          export: true,
          print: true,
          zoom: true,
          custom: true,
          slots: {
            buttons: "buttons",
          },
        },
        page: {
          param: {
            current: 1,
            size: this.$page.size,
            sord: "asc",
            sidx: "",
            id: "",
            filters: "",
          },
          total: 0,
          sizeOptions: this.$page.sizeOptions,
        },
        loading: true,
        data: [],
        height: window.innerHeight - 302,

        search: {
          option: {
            num: 4,
            config: [
              {
                field: "post.Name",
                op: "cn",
                placeholder: "请输入名称",
                title: "名称",
                value: null,
                type: "text",
              },
              {
                field: "post.Remark",
                op: "cn",
                placeholder: "请输入备注",
                title: "备注",
                value: null,
                type: "text",
              },
              {
                field: "post.IsFreeze",
                op: "eq",
                placeholder: "请选择禁用状态",
                title: "禁用",
                type: "bool",
              },
            ],
          },
        },
      },

      edit: {
        visible: false,
        postId: "",
        title: "",
        data: [],
      },

      visible: {
        update: false,
        del: false,
      },

      permission: {
        menu: {
          visible: false,
          title: "",
        },

        menuButton: {
          visible: false,
          title: "",
        },

        data: {
          visible: false,
          title: "",
        },

        menuMenuButtonData: {
          visible: false,
          title: "",
        },
      },
    };
  },
  mounted() {
    //表格和工具栏进行关联
    this.$refs.table.connect(this.$refs.toolbar);
    this.treeInit();
    this.tableInit();
  },
  methods: {
    /**
     * 菜单树
     */
    treeInit() {
      let _this = this;
      tree().then((result) => {
        _this.tree.loading = false;
        _this.tree.data = result.data;
        _this.edit.data = result.data;
      });
    },

    /**
     * 树图标
     */
    treeIcon(props) {
      const { expanded } = props;
      if (props.children.length == 0) {
        return <a-icon type="file" />;
      }
      return <a-icon type={expanded ? "folder-open" : "folder"} />;
    },

    /**
     * 树选中
     */
    treeSelect(electedKeys, e) {
      this.table.page.param.id = electedKeys[0];
      this.table.page.param.current = 1;
      this.tableInit();
    },

    /**
     * 列表提示
     */
    tableTooltip({ type, column, row, items, _columnIndex }) {
      const { property } = column;
      if (row && property === "parentIdsName") {
        return row[property];
      }
      return null;
    },

    /**
     * 列表数据
     */
    tableInit() {
      let _this = this;
      this.table.loading = true;
      this.table.page.param.filters = this.$refs.search.getFilters();
      query(_this.table.page.param).then((result) => {
        if (result.code == _this.$resultCode.success) {
          _this.table.data = result.data;
          _this.table.page.total = result.count;
        }
        _this.table.loading = false;
      });
    },

    /**
     * 列表排序改变
     */
    tableSort({ column, sidx, sord }) {
      this.table.page.param.current = 1;
      this.table.page.param.sidx = sidx;
      this.table.page.param.sord = sord;
      this.tableInit();
    },
    /**
     *数量改变
     */
    tableSizeChange(current, pageSize) {
      this.table.page.param.size = pageSize;
      this.tableInit();
    },
    /**
     * 列表查询
     */
    tableSearch() {
      this.table.page.param.current = 1;
      this.tableInit();
    },
    /**
     * 权限按钮加载完毕
     */
    toolbarOnload(buttons) {
      this.visible.add = buttons.filter((f) => f.method == "add").length > 0;
      this.visible.update =
        buttons.filter((f) => f.method == "update").length > 0;
      this.visible.del = buttons.filter((f) => f.method == "del").length > 0;
    },

    /**
     * 树更新
     */
    tableUpdateRow(row) {
      this.edit.title = "岗位编辑-" + row.name;
      this.edit.visible = true;
      this.$refs.edit.find(row.postId);
    },
    /**
     * 删除
     */
    tableDelRow(row) {
      let _this = this;
      deleteConfirm(
        "岗位【" + row.name + "】" + _this.$msg.delete,
        function() {
          _this.$message.loading(_this.$msg.delloading);
          del({ id: row.postId }).then((result) => {
            if (result.code == _this.$resultCode.success) {
              _this.reload(false);
            }
            _this.$message.destroy();
            _this.$message.success(result.msg);
          });
        },
        _this
      );
    },
    /**
     * 编辑关闭
     */
    close() {
      this.edit.visible = false;
    },

    /**
     * 新增
     */
    add() {
      this.edit.title = "新增岗位";
      this.edit.visible = true;
    },

    /**
     * 修改
     */
    update() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.edit.title = "编辑岗位-" + row.name;
          _this.edit.visible = true;
          _this.$refs.edit.find(row.postId);
        },
        _this
      );
    },

    /**
     * 删除
     */
    del() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(rows) {
          //提示是否删除
          deleteConfirm(
            _this.$msg.delete,
            function() {
              //加载提示
              _this.$message.loading(_this.$msg.delloading);
              let ids = XEUtils.map(rows, (item) => item.postId);
              del({ id: ids.join(",") }).then((result) => {
                if (result.code == _this.$resultCode.success) {
                  _this.reload(false);
                }
                _this.$message.destroy();
                _this.$message.success(result.msg);
              });
            },
            _this
          );
        },
        _this,
        false
      );
    },

    //提示状态信息
    perateStatus(result) {
      if (result.code === this.$resultCode.success) {
        this.edit.visible = false;
        this.reload(false);
      }
    },

    /**
     * 重新加载
     */
    reload(reset) {
      if (reset) this.table.page.param.id = this.$gemptyGuid;
      this.table.page.param.current = 1;
      this.treeInit();
      this.tableInit();
    },

    /**
     * 选择岗位
     */
    postUser() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.toolbar.chosenuser.visible = true;
          _this.toolbar.chosenuser.title = "岗位岗位-" + row.name;
          _this.$refs.chosenuser.userInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    /**
     * 选择岗位关闭
     */
    chosenUserClose() {
      this.toolbar.chosenuser.visible = false;
    },

    /**
     * 菜单权限
     */
    menu() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.permission.menu.visible = true;
          _this.permission.menu.title = "岗位模块权限-" + row.name;
          _this.$refs.permissionMenu.menuInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    /**
     * 菜单权限关闭
     */
    permissionMenuClose() {
      this.permission.menu.visible = false;
    },

    /**
     * 岗位按钮
     */
    menuButton() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.permission.menuButton.visible = true;
          _this.permission.menuButton.title = "岗位按钮权限-" + row.name;
          _this.$refs.permissionMenuButton.menuInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    menuButtonData() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.permission.menuButton.visible = true;
          _this.permission.menuButton.title = "岗位按钮权限-" + row.name;
          _this.$refs.permissionMenuButton.menuInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    /**
     * 岗位按钮关闭
     */
    permissionMenuButtonClose() {
      this.permission.menuButton.visible = false;
    },

    /**
     * 岗位菜单,按钮,数据权限
     */
    menuMenuButtonData() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.permission.menuMenuButtonData.visible = true;
          _this.permission.menuMenuButtonData.title = "岗位权限-" + row.name;
          _this.$refs.permissionMenuMenuButtonData.menuInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    /**
     * 岗位权限关闭
     */
    permissionMenuMenuButtonDataClose() {
      this.permission.menuMenuButtonData.visible = false;
    },

    /**
     * 数据权限
     */
    data() {
      let _this = this;
      selectTableRow(
        _this.$refs.table,
        function(row) {
          _this.permission.data.visible = true;
          _this.permission.data.title = "岗位数据权限-" + row.name;
          _this.$refs.permissionData.menuInit(
            _this.$privilegeMaster.post,
            row.postId
          );
        },
        _this
      );
    },

    /**
     * 数据权限关闭
     */
    permissionDataClose() {
      this.permission.data.visible = false;
    },
  },
};
</script>

<style lang="less" scoped></style>
